#include "D3DCamera.h"
#include "ShaderGroup.h"

D3DCamera::D3DCamera()
{
	XMMATRIX mat = XMMatrixIdentity();
	XMStoreFloat4x4(&viewMatrix, mat);
	XMStoreFloat4x4(&rotationMatrix, mat);


	
}


D3DCamera::~D3DCamera()
{
}


void D3DCamera::MoveCamera(double dt)
{
	XMMATRIX camera = XMLoadFloat4x4(&viewMatrix);
	camera = XMMatrixInverse(&XMMatrixDeterminant(camera), camera);
	float moveSpeed = 4.0f;

	if ((GetAsyncKeyState(VK_SHIFT)))
		moveSpeed *= 2.0f;
	



	if ((GetAsyncKeyState((int)'W')))
	{
		XMMATRIX translate = XMMatrixTranslation(0.0f, 0.0f, (moveSpeed * (float)dt));
		camera = XMMatrixMultiply(translate, camera);

	}

	if ((GetAsyncKeyState((int)'S')))
	{
		XMMATRIX translate = XMMatrixTranslation(0.0f, 0.0f, (-moveSpeed * (float)dt));
		camera = XMMatrixMultiply(translate, camera);
	}

	if ((GetAsyncKeyState((int)'A')))
	{
		XMMATRIX translate = XMMatrixTranslation((-moveSpeed * (float)dt), 0.0f, 0.0f);
		camera = XMMatrixMultiply(translate, camera);

	}
	if ((GetAsyncKeyState((int)'D')))
	{
		XMMATRIX translate = XMMatrixTranslation((moveSpeed * (float)dt), 0.0f, 0.0f);
		camera = XMMatrixMultiply(translate, camera);
	}

	if ((GetAsyncKeyState(VK_UP)))
	{
		XMMATRIX translate = XMMatrixTranslation(0.0f, (moveSpeed * (float)dt), 0.0f);
		camera = XMMatrixMultiply(translate, camera);
	}

	if ((GetAsyncKeyState(VK_DOWN)))
	{
		XMMATRIX translate = XMMatrixTranslation(0.0f, -(moveSpeed * (float)dt), 0.0f);
		camera = XMMatrixMultiply(translate, camera);

	}


	if (GetAsyncKeyState(VK_NUMPAD8))
	{
		XMMATRIX rotation = XMMatrixRotationX(-((XM_PI * 0.5f) *(float)dt));
		camera = XMMatrixMultiply(rotation, camera);
		XMMATRIX rot = XMLoadFloat4x4(&rotationMatrix);
		rot = XMMatrixMultiply(rotation, rot);
		XMStoreFloat4x4(&rotationMatrix, rot);

		//rotationQuaternion = XMFLOAT4(-((XM_PI * 0.5f) *(float)dt), 0.0f, 0.0f, 1.0f);


	}

	if (GetAsyncKeyState(VK_NUMPAD2))
	{
		XMMATRIX rotation = XMMatrixRotationX(((XM_PI * 0.5f) *(float)dt));
		camera = XMMatrixMultiply(rotation, camera);
		XMMATRIX rot = 	XMLoadFloat4x4(&rotationMatrix);
		rot = XMMatrixMultiply(rotation, rot);
		XMStoreFloat4x4(&rotationMatrix, rot);
		//rotationQuaternion = XMFLOAT4(((XM_PI * 0.5f) *(float)dt), 0.0f, 0.0f, 1.0f);

	}
	if (GetAsyncKeyState(VK_NUMPAD4))
	{

		XMMATRIX rotation = XMMatrixRotationY(-((XM_PI * 0.5f) *(float)dt));
		camera = XMMatrixMultiply(rotation, camera);
		XMMATRIX rot = XMLoadFloat4x4(&rotationMatrix);
		rot = XMMatrixMultiply(rotation, rot);
		XMStoreFloat4x4(&rotationMatrix, rot);
		//rotationQuaternion = XMFLOAT4(0.0f, -((XM_PI * 0.5f) *(float)dt), 0.0f, 1.0f);


	}

	if (GetAsyncKeyState(VK_NUMPAD6))
	{
		XMMATRIX rotation = XMMatrixRotationY(((XM_PI * 0.5f) *(float)dt));
		camera = XMMatrixMultiply(rotation, camera);
		XMMATRIX rot = XMLoadFloat4x4(&rotationMatrix);
		rot = XMMatrixMultiply(rotation, rot);
		XMStoreFloat4x4(&rotationMatrix, rot);
		//rotationQuaternion = XMFLOAT4(0.0f, ((XM_PI * 0.5f) *(float)dt), 0.0f, 1.0f);


	}


	camera = XMMatrixInverse(&XMMatrixDeterminant(camera), camera);
	XMStoreFloat4x4(&viewMatrix, camera);

}

void D3DCamera::SetMatrix(XMFLOAT4X4 newMatrix)
{
	viewMatrix = newMatrix;
}

void D3DCamera::RotateLight(SPOT_LIGHT_BUFFER* buffer)
{
	XMVECTOR vec = XMLoadFloat4(&direction);
	XMMATRIX mat = XMLoadFloat4x4(&rotationMatrix);
	
	XMVECTOR newDirection = XMVector3TransformCoord(vec, mat);

	newDirection = XMVector3Normalize(newDirection);

//	XMVECTOR coneDir = XMLoadFloat4(&buffer->coneDir);
////	XMVECTOR rotate = XMLoadFloat4(&rotationQuaternion);
//	coneDir = XMVector3Rotate(coneDir, rotate);

	XMStoreFloat4(&buffer->coneDir, newDirection);
	//rotationMatrix = XMMatrixIdentity();

	if ((GetAsyncKeyState('F') & 0x1))
	{
		if (lightOn)
			buffer->slColor = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
		else
			buffer->slColor = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f);
			
		lightOn = !lightOn;
	}
}

